package com.rongshu.view.home.fragment.dialog;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.home.adapter.CatalogAdapter;
import com.rongshu.view.home.contract.CatalogueView;
import com.rongshu.view.home.entity.CatalogInfo;
import com.rongshu.view.home.listener.OnItemClickListener;
import com.rongshu.view.home.presenter.CataloguePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.utils.MeasureUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.rongshu.utils.UserInfoUtils.getUserId;


public class CatalogueDialog extends Dialog {
    @BindView(R.id.txt_catalog)
    TextView txt_catalog;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private int defaultHeight = 487;//高度

    public CatalogueDialog(Context context) {
        super(context, R.style.full_width_dialog_at_screen_bottom);
        View view = View.inflate(context, R.layout.adapter_catalogue_dialog_view, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        WindowManager.LayoutParams layoutParams = this.getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = (int) MeasureUtils.dp2px(getContext(), defaultHeight);
        setCanceledOnTouchOutside(true);
    }

    @OnClick({R.id.iv_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
        }
    }

    public static class Builder implements CatalogueView, OnItemClickListener {
        private Context context;
        private CatalogueDialog dialog;
        //serialize_id
        private int serialize_id = 0;
        //presenter
        private CataloguePresenter presenter = null;
        //manager
        private LinearLayoutManager manager = null;
        //adapter
        private CatalogAdapter mCatalogAdapter = null;
        private int pageIndex = 1;
        private int pageSize = 10;
        //refresh,load
        private boolean isrefresh = true;
        //map
        private Map<String, Object> map = null;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setSerialize_id(int serialize_id) {
            this.serialize_id = serialize_id;
            return this;
        }

        @Override
        public void onCatalogueMsg(CatalogInfo catalogInfo) {
            if (catalogInfo == null) {
                return;
            }
            //title
            dialog.txt_catalog.setText("目录（共" + catalogInfo.getTotal() + "篇）");
            List<CatalogInfo.ResponseBean> catalogueBeanList = catalogInfo.getResponse();
            if (mCatalogAdapter != null) {
                if (isrefresh) {
                    dialog.smartRefreshLayout.finishRefresh();
                    mCatalogAdapter.setCatalogInfoList(catalogueBeanList);
                    mCatalogAdapter.notifyDataSetChanged();
                } else {
                    if (catalogueBeanList == null || catalogueBeanList.isEmpty()) {
                        dialog.smartRefreshLayout.finishLoadmoreWithNoMoreData();
                    } else {
                        dialog.smartRefreshLayout.finishLoadmore();
                        mCatalogAdapter.addCatalogInfoList(catalogueBeanList);
                        mCatalogAdapter.notifyDataSetChanged();
                    }
                }
            }
        }

        @Override
        public void onItemClick(int position) {
            if (mCatalogAdapter == null) {
                return;
            }
            int id = mCatalogAdapter.getCatalogInfoList().get(position).getContribute_id();
            Intent intent = new Intent(context, SingleArticleDetailsActivity.class);
            ArticleDetaisRequest request = new ArticleDetaisRequest();
            request.setContribute_id(id);
            request.setType(1);
            request.setTopic_id(0);
            intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, request);
            context.startActivity(intent);
            dialog.dismiss();
        }

        public CatalogueDialog create() {
            if (dialog == null) {
                dialog = new CatalogueDialog(context);
            }
            //presenter
            presenter = new CataloguePresenter(this);
            //manager
            manager = new LinearLayoutManager(context);
            //adapter
            mCatalogAdapter = new CatalogAdapter(context);
            mCatalogAdapter.setOnItemClickListener(this);
            //title
            dialog.txt_catalog.setText("目录");
            //recyclerview
            dialog.recyclerView.setLayoutManager(manager);
            dialog.recyclerView.setAdapter(mCatalogAdapter);
            //refresh
            dialog.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    isrefresh = true;
                    pageIndex = 1;
                    refreshlayout.resetNoMoreData();
                    getCatalogue();
                }
            });

            dialog.smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
                @Override
                public void onLoadmore(RefreshLayout refreshlayout) {
                    isrefresh = false;
                    pageIndex++;
                    getCatalogue();
                }
            });
            dialog.smartRefreshLayout.autoRefresh();
            return dialog;
        }

        /**
         * 连载目录
         */
        private void getCatalogue() {
            int user_id = getUserId();
            map = new HashMap<>();
            map.put("user_id", user_id);
            map.put("serialize_id", serialize_id);
            map.put("pageIndex", pageIndex);
            map.put("pageSize", pageSize);
            presenter.getCatalogue(
                    (int) map.get("user_id"),
                    (int) map.get("serialize_id"),
                    (int) map.get("pageIndex"),
                    (int) map.get("pageSize"),
                    Common.encryptionSign(map));
            map.clear();
        }
    }
}
